package com.ruoyi.wechat.service.impl;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.wechat.mapper.DetailsMapper;
import com.ruoyi.wechat.service.IDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wechat.mapper.AppointmentMapper;
import com.ruoyi.wechat.domain.Appointment;
import com.ruoyi.wechat.service.IAppointmentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约管理Service业务层处理
 * 
 * @author mu
 * @date 2024-11-20
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService 
{
    @Autowired
    private AppointmentMapper appointmentMapper;

    /**
     * 查询预约管理
     * 
     * @param appointId 预约管理主键
     * @return 预约管理
     */
    @Override
    public Appointment selectAppointmentByAppointId(String appointId)
    {
        return appointmentMapper.selectAppointmentByAppointId(appointId);
    }

    /**
     * 查询预约管理列表
     * 
     * @param appointment 预约管理
     * @return 预约管理
     */
    @Override
    public List<Appointment> selectAppointmentList(Appointment appointment)
    {
        return appointmentMapper.selectAppointmentList(appointment);
    }

    @Autowired
    private DetailsMapper detailsMapper;
    /**
     * 新增预约管理
     * 
     * @param appointment 预约管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAppointment(Appointment appointment)
    {
        //给预约id赋值
        appointment.setAppointId(UUID.randomUUID().toString());
        appointment.setState("0");
        appointment.setQrcode("xxx");
        if(appointment.getDetailsList()!=null &&  appointment.getDetailsList().size()>0){
            //新增预约详情
            for (int i = 0; i < appointment.getDetailsList().size(); i++) {
                appointment.getDetailsList().get(i).setAppointId(appointment.getAppointId());
                appointment.getDetailsList().get(i).setDetailId(IdUtils.randomUUID());
                detailsMapper.insertDetails(appointment.getDetailsList().get(i));
            }
        }
        //新增预约
        return appointmentMapper.insertAppointment(appointment);
    }

    /**
     * 修改预约管理
     * 
     * @param appointment 预约管理
     * @return 结果
     */
    @Override
    public int updateAppointment(Appointment appointment) {
        //循环添加预约详情
        if(appointment.getDetailsList()!=null &&  appointment.getDetailsList().size()>0){
            //根据预约id删除详情
            detailsMapper.deleteDetailsByAppointId(appointment.getAppointId());
            for (int i = 0; i < appointment.getDetailsList().size(); i++) {
                appointment.getDetailsList().get(i).setAppointId(appointment.getAppointId());
                appointment.getDetailsList().get(i).setDetailId(IdUtils.randomUUID());
                detailsMapper.insertDetails(appointment.getDetailsList().get(i));
            }
        }
        //更新预约
        return appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 批量删除预约管理
     * 
     * @param appointIds 需要删除的预约管理主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByAppointIds(String[] appointIds)
    {
        return appointmentMapper.deleteAppointmentByAppointIds(appointIds);
    }

    /**
     * 删除预约管理信息
     * 
     * @param appointId 预约管理主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByAppointId(String appointId)
    {
        return appointmentMapper.deleteAppointmentByAppointId(appointId);
    }
}
